/**
 * Created by 97203 on 2018/06/08.
 */
$(function () {
    var clientdevplan_datagrid = $("#clientdevplan_datagrid");
    var clientdevplan_dialog = $("#clientdevplan_dialog");
    var client_datagrid = $("#client_datagrid");
    var result_dialog = $("#result_dialog");
    var client_dialog = $("#client_dialog");
    var clientdevplan_form = $("#clientdevplan_form");
    var result_form = $("#result_form");
    clientdevplan_datagrid.datagrid({
        url: '/clientdevplan/query.do',
        fitColumns: true,
        fit: true,
        pagination: true,    //分页条
        rownumbers: true,     //行号列
        singleSelect: true,
        toolbar: '#tb',
        columns: [[
            {field: 'id', title: '编号', width: 100},
            {
                field: 'traceResult', title: '执行结果', width: 100, formatter: function (value) {
                if (value > 0 && value == 1) {
                    return "差";
                } else if (value == 2) {
                    return "中";
                } else {
                    return "优";
                }
            }
            },
            {field: 'planTime', title: '计划时间', width: 100},
            {
                field: 'potentialClient', title: '潜在客户', width: 100, formatter: function (value) {
                return value ? value.name : '';
            }
            },
            {field: 'planSubject', title: '计划主题', width: 100},
            {
                field: 'planType', title: '实施方式', width: 100, formatter: function (value) {
                return value ? value.name : '';
            }
            },
            {
                field: 'inputUser', title: '创建人', width: 100, formatter: function (value) {
                return value ? value.username : '';
            }
            },
            {field: 'inputTime', title: '创建时间', width: 100,},
            {field: 'planDetail', title: '详情', width: 100},
            {field: 'remark', title: '备注', width: 100},
        ]]
    })
    client_datagrid.datagrid({
        url: '/client/potentialCustomerList.do?status=0',
        fitColumns: true,
        fit: true,
        pagination: true,    //分页条
        rownumbers: true,     //行号列
        singleSelect: true,
        columns: [[
            {
                field: 'inChargeUser', title: '负责人', width: 100, formatter: function (value) {
                return value ? value.username : '';
            }
            },
            {field: 'name', title: '客户姓名', width: 100},
            {
                field: 'gender', title: '性别', width: 100, formatter: function (value) {
                if (value) {
                    return '男';
                } else {
                    return '女';
                }
            }
            },
            {field: 'tel', title: '电话', width: 100},
            {field: 'email', title: '邮箱', width: 100,},
            {field: 'address', title: '客户地址', width: 100},
            {field: 'idNo', title: '身份证号', width: 100},
        ]]
    })
    clientdevplan_dialog.dialog({
        width: 600,
        height: 350,
        buttons: '#btn',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            clientdevplan_form.form('clear');
        }
    })
    client_dialog.dialog({
        width: 900,
        height: 500,
        buttons: '#client_btn',
        closed: true
    })
    result_dialog.dialog({
        width: 300,
        height: 250,
        buttons: '#result_btn',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            result_form.form('clear');
        }
    })

    var methodObj = {
        add: function () {
            //设置标题
            clientdevplan_dialog.dialog('setTitle', '新增开发计划');
            //打开弹出框
            clientdevplan_dialog.dialog('open');
        },
        edit: function () {
            var row = clientdevplan_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要编辑的数据！', 'warning');
                return;
            }
            if (row.potentialClient) {
                row["potentialClient.id"] = row.potentialClient.id;
                row["potentialClient.name"] = row.potentialClient.name;
            }
            if(row.planType){
                row["planType.id"] = row.planType.id;
            }
            //把数据回显到表单中
            clientdevplan_form.form('load', row);
            //设置标题
            clientdevplan_dialog.dialog('setTitle', '编辑开发计划');
            //打开弹出框
            clientdevplan_dialog.dialog('open');

        },
        changeResult: function () {
            var row = clientdevplan_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            result_form.form('load', row);
            result_dialog.dialog('setTitle', '标记执行结果');
            result_dialog.dialog('open');
            /*
             $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
             if (r) {
             $.get('/clientdevplan/changeResult.do', {id: row.id}, function (data) {
             if (data.success) {
             $.messager.alert('温馨提示', '操作成功！', 'info', function () {
             clientdevplan_datagrid.datagrid('reload');
             });
             } else {
             $.messager.alert('温馨提示', data.msg, 'warning');
             }
             })
             }
             });*/
        },
        reload: function () {
            clientdevplan_datagrid.datagrid('reload');
        },
        save: function () {
            //提交ajax
            clientdevplan_form.form('submit', {
                url: '/clientdevplan/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                            //关闭弹出框
                            methodObj.cancel();
                            //在dataGrid重新加载数据
                            clientdevplan_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'warning');
                    }
                }
            })
        },
        remove: function () {
            var row = clientdevplan_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
                if (r) {
                    $.get('/clientdevplan/delete.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                                clientdevplan_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        cancel: function () {
            clientdevplan_dialog.dialog('close');
        },
        query: function () {
            var beginTime = $("[name=beginTime]").val();
            var endTime = $("[name=endTime]").val();
            var clientName = $("[name=clientName]").val();
            var empName = $("[name=empName]").val();
            var subjectName = $("[name=subjectName]").val();
            var planTypeId = $("[name=planTypeId]").val();
            clientdevplan_datagrid.datagrid('load', {
                beginTime: beginTime,
                endTime: endTime,
                clientName: clientName,
                empName: empName,
                subjectName: subjectName,
                planTypeId: planTypeId
            })
        },
        resultUpdate: function () {
            result_form.form('submit', {
                url: '/clientdevplan/changeResult.do',
               onSubmit: function (param) {
                    var row = clientdevplan_datagrid.datagrid('getSelected');
                    param['id'] = row.id;
                },
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示', '操作成功', 'info', function () {
                            //关闭弹出框
                            methodObj.resultCancel();
                            //在dataGrid重新加载数据
                            clientdevplan_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'warning');
                    }
                }
            })
        },
        resultCancel: function () {
            result_dialog.dialog('close');
        },
        clientDialog: function () {
            client_dialog.dialog('setTitle', '请选择潜在客户');
            client_dialog.dialog('open');
        },
        contain: function () {
            var row = client_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $("#potentialClientName").textbox('setValue', row.name);
            $("#potentialClientId").val(row.id);
            client_dialog.dialog('close');
        }
    }
    $("a[data-cmd]").click(function () {
        //获取按钮上的data-cmd的值
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})
